package com.jnxg.smartcommunity.activity.user;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.geekbean.android.utils.GB_StringUtils;
import com.geekbean.android.utils.GB_VerificationUtils;
import com.jnxg.smartcommunity.R;
import com.jnxg.smartcommunity.activity.BaseActivity;
import com.jnxg.smartcommunity.manager.UrlManager;
import com.jnxg.smartcommunity.responseutils.ResponseUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Timer;

public class RegActivity extends BaseActivity implements View.OnClickListener {
    private EditText mobileEdit, codeEdit, inviteEdit, pwdEdit,
            repwdEdit;
    private Button codeBtn;
    private String mobile, pass, code;
    private Timer timer;
    int i = 30;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_reg);
        this.initFrame();
    }

    private void initFrame() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar 这是顶部的沉浸式
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        // Translucent navigation bar 这是底部的沉浸式
////            window.setFlags(
////                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
////                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        mobileEdit = (EditText) findViewById(R.id.edit_mobile);
        codeEdit = (EditText) findViewById(R.id.edit_code);
        pwdEdit = (EditText) findViewById(R.id.edit_pwd);
        repwdEdit = (EditText) findViewById(R.id.edit_repwd);
        codeBtn = (Button) findViewById(R.id.btn_code);
        codeBtn.setOnClickListener(this);
        findViewById(R.id.btn_reg).setOnClickListener(this);
        findViewById(R.id.btn_left_layout).setOnClickListener(this);
    }

    //发送验证码
    public void requestCode() {
        mobile = mobileEdit.getText().toString().trim();
        String url = UrlManager.getInstance().sendsms();
        OkHttpUtils
                .post()
                .url(url)
                .addParams("mobile", mobile)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e, int i) {
                        Toast.makeText(getBaseContext(), "请检查网络", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String s, int i) {
                        Log.i("tag", s);
//                        if(ResponseUtils.getCode(s)==200)  {
//                            String data = ResponseUtils.getDataStr(s);
//                            try {
//                                JSONObject obj = new  JSONObject(data);
//                                String token =obj.getString("token");
//                                Toast.makeText(getBaseContext(), token, Toast.LENGTH_SHORT).show();
//                                Contas.putToken(LoginActivity.this,token);
//                                Intent intent = new  Intent(LoginActivity.this, MainActivity.class);
//                                startActivity(intent);
//                                finish();
//                            }catch (Exception e){
//                            }
//                        }else{
//                            Toast.makeText(getBaseContext(), "密码或者账号错误", Toast.LENGTH_SHORT).show();
//                        }
                    }
                });
    }

    @Override
    public void onClick(View arg0) {
        if (arg0.getId() == R.id.btn_left_layout) {
            finish();
        }
        if (arg0.getId() == R.id.btn_code) {
            mobile = mobileEdit.getText().toString().trim();
            //判断手机号格式
            if (GB_StringUtils.isBlank(mobile)
                    || !GB_VerificationUtils.isMobilePhoneNumber(mobile)) {
                Toast.makeText(this, "手机号错误", Toast.LENGTH_SHORT).show();
            } else {
                requestCode();
                codeBtn.setClickable(false);
                codeBtn.setText("(" + i + ")" + "秒后重新发送");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (; i > 0; i--) {
                            handler.sendEmptyMessage(-9);
                            if (i <= 0) {
                                break;
                            }
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        handler.sendEmptyMessage(-8);
                    }
                }).start();
            }
        }
        //服务端注册
        if (arg0.getId() == R.id.btn_reg) {
//            if (GB_StringUtils.isBlank(mobileEdit.getText().toString())) {
//                Toast.makeText(this, getString(R.string.alert_user_reg_mobile_null), Toast.LENGTH_SHORT).show();
//                return;
//            }
//            if (GB_StringUtils.isBlank(codeEdit.getText().toString())) {
//                Toast.makeText(this, getString(R.string.alert_user_reg_code_null), Toast.LENGTH_SHORT).show();
//                return;
//            }
//            if (GB_StringUtils.isBlank(pwdEdit.getText().toString())) {
//                Toast.makeText(this, getString(R.string.alert_user_reg_pwd_null), Toast.LENGTH_SHORT).show();
//                return;
//            }
//            if (!pwdEdit.getText().toString()
//                    .equals(repwdEdit.getText().toString())) {
//                Toast.makeText(this, getString(R.string.alert_user_reg_repwd_error), Toast.LENGTH_SHORT).show();
//                return;
//            }
            Intent intent = new Intent(RegActivity.this, UserSelectActivity.class);
            startActivity(intent);
            // regHttpRequest();
        }
    }

    public void regHttpRequest() {
        mobile = mobileEdit.getText().toString().trim();
        pass = pwdEdit.getText().toString().trim();
        code = codeEdit.getText().toString().trim();
        String url = UrlManager.getInstance().register();
        OkHttpUtils
                .post()
                .url(url)
                .addParams("code", code)
                .addParams("mobile", mobile)
                .addParams("password", pass)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e, int i) {
                        Toast.makeText(getBaseContext(), "请检查网络", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String s, int i) {
                        Log.i("tag", s);
                        String msg = ResponseUtils.getMsg(s);
                        if (ResponseUtils.getCode(s) == 1) {
                            Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
                            try {

                            } catch (Exception e) {
                            }
                        } else {
                            Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == -9) {
                codeBtn.setText("(" + i + ")" + "秒后重新发送");
                codeBtn.setTextColor(getResources().getColor(R.color.colorHui));
            } else if (msg.what == -8) {
                codeBtn.setText("获取验证码");
                codeBtn.setTextColor(getResources().getColor(R.color.colorWhite));
                codeBtn.setClickable(true);
                i = 30;
            }
        }
    };
}
